#use "test_pp_parser.ml";;

let p = create ();;

let p = post 1 'X' p;;
let p = post 2 'A' p;;
let p = post 3 'B' p;;
let p = post 4 'C' p;;
let p = post 5 '\x1a' p;; (* eof *)

flush stderr;;

let pp_elt = Castling.CharToken.pp ~paren:true ~quali:false;;

let pp_pos f p = (
	if p < 0 then Format.pp_print_char f '(';
	Format.pp_print_int f p;
	if p < 0 then Format.pp_print_char f ')'
);;

if accepted p then (
	pp_option (pp_result pp_elt pp_pos) ~paren:false Format.str_formatter (result p);
	let image = Format.flush_str_formatter () in
	if image = "Some (Some (X (1, Some 'X')), Some (2, Some 'A', (3, Some 'B'), 4))" then (
		begin match result p with
		| Some (Some (X (1, Some 'X')), Some (2, Some 'A', (3, Some 'B'), 4)) ->
			begin match errors p with
			| [] ->
				print_string "OK.\n";
			| _ ->
				raise (Failure "BAD ERRORS DETECTED!!")
			end
		| _ ->
			raise (Failure "BAD RESULT!!")
		end
	) else (
		raise (Failure "BAD IMAGE!!")
	)
) else (
	raise (Failure "NOT ACCEPTED!!")
);;
